//
//  MultipleViewSelectedLayout.m
//  多View选择器
//
//  Created by LK-IOS on 2018/1/4.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "MultipleViewSelectedLayout.h"
#import "MultipleViewSelectedProperty.h"
#import "MultipleViewSelectedCellModel.h"
#import "UIView+Multiple.h"
#import "UIColor+Multiple.h"
#import "NSString+Multiple.h"



@interface MultipleViewSelectedLayout()


@property (nonatomic, copy) NSMutableArray *attrs;
@property (nonatomic, assign) CGFloat contentWidth;
@property (nonatomic, assign) CGFloat totleTitleWidth;
@property (nonatomic, assign) CGFloat totleCenterX;
@property (nonatomic, assign) BOOL needScroll;
@property (nonatomic, assign) CGFloat realItemSpacing;

@end


@implementation MultipleViewSelectedLayout


- (void)prepareLayout
{
    [super prepareLayout];
    _contentWidth = 0;
    _realItemSpacing = _property.itemSpacing;
    NSString * allTitles = [_property.titles componentsJoinedByString:@""];
    _totleTitleWidth = [allTitles pz_sizeWithfont:_property.titleFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width;
    _contentWidth = _totleTitleWidth + _property.edgeSpacing * 2 + _realItemSpacing * (_property.data.count - 1);
    _needScroll = _contentWidth > self.collectionView.width;
    if (!_needScroll) {
        _realItemSpacing = (self.collectionView.bounds.size.width - _totleTitleWidth - _property.edgeSpacing * 2) / (float)(_property.data.count - 1);
        _contentWidth = self.collectionView.width;
    }
    _totleCenterX = _property.edgeSpacing - _realItemSpacing;
    _attrs = @[].mutableCopy;
    for (int i = 0; i < _property.data.count; i++) {
        [_attrs addObject:[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]];
    }
}

/**
 返回正确的contentsize
 */
- (CGSize)collectionViewContentSize
{
    return CGSizeMake(_contentWidth, self.collectionView.bounds.size.height);
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return [self pz_itemLayoutAttributesPathInRect:rect];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    MultipleViewSelectedCellModel *model = _property.data[indexPath.item];
    //计算每个item的size
    CGSize size = [model.title pz_sizeWithfont:_property.titleFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    attr.size = size;
    model.cellSize = size;
    //计算每个item的center
    CGFloat centerX = _totleCenterX + _realItemSpacing + size.width / 2.0f;
    _totleCenterX = centerX + size.width / 2.0f;
    CGPoint center = CGPointMake(centerX, self.collectionView.height / 2.0f);
    if (_property.data.count < 2) {
        center = CGPointMake(self.collectionView.width / 2.0f, self.collectionView.height / 2.0f);
    }
    model.cellCenter = center;
    attr.center = center;
    return attr;
}

//此处比较慢，有合适方法再说
- (NSArray<UICollectionViewLayoutAttributes *> *)pz_itemLayoutAttributesPathInRect:(CGRect)rect
{
    NSMutableArray *temp = @[].mutableCopy;
    for (int i = 0; i < _attrs.count; i ++) {
        UICollectionViewLayoutAttributes *attr = _attrs[i];
        
        if (CGRectContainsRect(rect, attr.frame) || CGRectIntersectsRect(rect, attr.frame)) {
            [temp addObject:attr];
        }
    }
    return temp.copy;
}


@end
